package com.creative.accountsdk.holder;

import android.app.Dialog;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.R;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.BindProtocol;
import com.creative.accountsdk.net.account.ModifyPasswordProtocol;
import cz.msebera.android.httpclient.Header;
import org.json.JSONObject;

/**
 * Created by yinjialiang on 15-6-24.
 */
public class ModifyPasswordHolder extends BaseHolder{
    private EditText editTextOldPassword, editTextPassword, editTextPassword2;

    @Override
    protected void initViews() {
        editTextOldPassword = (EditText) viewRoot.findViewById(R.id.editTextOldPassword);
        editTextPassword = (EditText) viewRoot.findViewById(R.id.editTextPassword);
        editTextPassword2 = (EditText) viewRoot.findViewById(R.id.editTextPassword2);

        viewRoot.findViewById(R.id.buttonOK).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bind();
            }
        });
    }

    private void bind()
    {
        String oldpassword = editTextOldPassword.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();
        String password2 = editTextPassword2.getText().toString().trim();
        if (oldpassword.equals("")) {
            Toast.makeText(activity, R.string.account_old_password_empty, Toast.LENGTH_SHORT).show();
        } else if (password.equals("") || password2.equals("")) {
            Toast.makeText(activity, R.string.account_password_empty, Toast.LENGTH_SHORT).show();
        } else if (!password.equals(password2)) {
            Toast.makeText(activity, R.string.account_password_diff, Toast.LENGTH_SHORT).show();
        } /*else if (oldpassword.length() < 6) {
            Toast.makeText(activity, R.string.account_old_password_length, Toast.LENGTH_SHORT).show();
        } else if (password.length() < 6) {
            Toast.makeText(activity, R.string.account_password_length, Toast.LENGTH_SHORT).show();
        } */else {
            final Dialog progress = DialogUtils.createProgress(activity);
            progress.show();
            final ModifyPasswordProtocol protocol = new ModifyPasswordProtocol(oldpassword, password);
            protocol.debugUrl(DataCenter.getHttpClient());
            DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    progress.cancel();
                    if (protocol.parse(response, activity)) {
                        activity.finish();
                    }
                }
            });
        }
    }

    @Override
    public int getLayoutResID() {
        return R.layout.account_modify_password;
    }
}
